package com.mantru.iotpot.Begin;

import android.app.Dialog;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.ContextCompat;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import com.mantru.iotpot.ApplicationClass;
import com.mantru.iotpot.Home.HomeActivity;
import com.mantru.iotpot.R;
import com.mantru.iotpot.eventbus.EventMessage;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

public class ManualCheakActivity extends AppCompatActivity implements View.OnClickListener, View.OnTouchListener {

    private LinearLayout box1;
    private LinearLayout box2;
    private LinearLayout box3;
    private Button up;
    private Button down;
    private TextView condition1;
    private Button reset;
    private TextView condition2;
    private TextView condition3;
    private TextView condition4;
    private TextView condition5;
    private Button front;
    private Button after;
    private Button magazine_reset;
    private Button first_next;
    private Button arm_up;
    private Button arm_down;
    private Button pot_up;
    private Button pot_down;
    private Button clockwise;
    private Button anticlockwise;
    private Button arm_reset;
    private Button pot_reset;
    private Button second_previous;
    private Button second_next;
    private Button rotate;
    private Button third_previous;
    private ApplicationClass ac;
    private Dialog dialog;
    private int cheak = 0;//1表示成功，2表示超时

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_manual_cheak);
        ac = (ApplicationClass) getApplication();// 初始化 ApplicationClass
        initialization();//初始化控件
        EventBus.getDefault().register(this);
    }

    //初始化控件的方法
    private void initialization(){
        box1 = findViewById(R.id.manual_cheak_box1);
        box2 = findViewById(R.id.manual_cheak_box2);
        box3 = findViewById(R.id.manual_cheak_box3);

        condition1 = findViewById(R.id.manual_cheak_condition_text1);
        condition2 = findViewById(R.id.manual_cheak_condition_text2);
        condition3 = findViewById(R.id.manual_cheak_condition_text3);
        condition4 = findViewById(R.id.manual_cheak_condition_text4);
        condition5 = findViewById(R.id.manual_cheak_condition_text5);

        up = findViewById(R.id.manual_cheak_up_button);
        down = findViewById(R.id.manual_cheak_down_button);
        reset = findViewById(R.id.manual_cheak_reset_button);
        front = findViewById(R.id.manual_cheak_front_button);
        after = findViewById(R.id.manual_cheak_after_button);
        magazine_reset = findViewById(R.id.manual_cheak_magazine_reset_button);
        first_next = findViewById(R.id.manual_cheak_button1);
        arm_up = findViewById(R.id.manual_cheak_arm_up_button);
        arm_down = findViewById(R.id.manual_cheak_arm_dowm_button);
        pot_up = findViewById(R.id.manual_cheak_pot_up_button);
        pot_down = findViewById(R.id.manual_cheak_pot_down_button);
        clockwise = findViewById(R.id.manual_cheak_clockwise_button);
        anticlockwise = findViewById(R.id.manual_cheak_anticlockwise_button);
        arm_reset = findViewById(R.id.manual_cheak_arm_reset_button);
        pot_reset = findViewById(R.id.manual_cheak_pot_reset_button);
        second_previous = findViewById(R.id.manual_cheak_button2);
        second_next = findViewById(R.id.manual_cheak_button3);
        rotate = findViewById(R.id.manual_cheak_rotate_button);
        third_previous = findViewById(R.id.manual_cheak_button4);

        up.setOnTouchListener(this);
        down.setOnTouchListener(this);
        front.setOnTouchListener(this);
        after.setOnTouchListener(this);
        arm_up.setOnTouchListener(this);
        arm_down.setOnTouchListener(this);
        pot_up.setOnTouchListener(this);
        pot_down.setOnTouchListener(this);
        clockwise.setOnTouchListener(this);
        anticlockwise.setOnTouchListener(this);

        reset.setOnClickListener(this);
        magazine_reset.setOnClickListener(this);
        first_next.setOnClickListener(this);
        arm_reset.setOnClickListener(this);
        pot_reset.setOnClickListener(this);
        second_previous.setOnClickListener(this);
        second_next.setOnClickListener(this);
        rotate.setOnClickListener(this);
        third_previous.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.manual_cheak_reset_button) {//投菜复位
            ac.Device_SendCommand_HandCheck_Image1(1, 3);
            ac.SaveLog("用户按下投菜复位按钮，event:1,type:3");
        }else if (v.getId() == R.id.manual_cheak_magazine_reset_button) {//料盒复位
            ac.Device_SendCommand_HandCheck_Image2(1, 3);
            ac.SaveLog("用户按下料盒复位按钮，event:1,type:3");
        }else if (v.getId() == R.id.manual_cheak_button1) {//第一页的下一页
            box1.setVisibility(View.GONE);
            box2.setVisibility(View.VISIBLE);
        }else if (v.getId() == R.id.manual_cheak_arm_reset_button) {//机械臂复位
            ac.Device_SendCommand_HandCheck_Image345(1, 7);
            ac.SaveLog("用户按下机械臂复位按钮，event:1,type:7");
        }else if (v.getId() == R.id.manual_cheak_pot_reset_button) {//锅复位
            ac.Device_SendCommand_HandCheck_Image345(1, 8);
            ac.SaveLog("用户按下锅复位按钮，event:1,type:8");
        }else if (v.getId() == R.id.manual_cheak_button2) {//第二页的上一页
            box2.setVisibility(View.GONE);
            box1.setVisibility(View.VISIBLE);
        }else if (v.getId() == R.id.manual_cheak_button3) {//第二页的下一页
            box2.setVisibility(View.GONE);
            box3.setVisibility(View.VISIBLE);
        }else if (v.getId() == R.id.manual_cheak_rotate_button) {//自传寻位
            ac.Device_SendCommand_HandCheck_Page3();
            ac.SaveLog("用户按下自传寻位按钮");
        }else if (v.getId() == R.id.manual_cheak_button4) {//第三页的上一页
            box3.setVisibility(View.GONE);
            box2.setVisibility(View.VISIBLE);
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        box1.setVisibility(View.VISIBLE);
        box2.setVisibility(View.GONE);
        box3.setVisibility(View.GONE);
    }

    @Override
    protected void onDestroy() {
        EventBus.getDefault().unregister(this);
        super.onDestroy();
    }

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        //向上
        if (v.getId() == R.id.manual_cheak_up_button){
            if (event.getAction() == MotionEvent.ACTION_DOWN) {
                ac.Device_SendCommand_HandCheck_Image1(1, 1);
                ac.SaveLog("用户按下向上按钮，event:1,type:1");
                up.setBackgroundResource(R.drawable.button11);
                return true;
            } else if (event.getAction() == MotionEvent.ACTION_UP || event.getAction() == MotionEvent.ACTION_CANCEL || event.getAction() == MotionEvent.ACTION_OUTSIDE) {
                ac.Device_SendCommand_HandCheck_Image1(2, 1);
                ac.SaveLog("用户松开向上按钮，event:2,type:1");
                up.setBackgroundResource(R.drawable.button13);
                return true;
            } else if (event.getAction() == MotionEvent.ACTION_MOVE) {
                // 忽略移动事件，不执行任何操作
                return true;
            }
            return false;
        }
        //向下
        else if (v.getId() == R.id.manual_cheak_down_button) {
            if (event.getAction() == MotionEvent.ACTION_DOWN) {
                ac.Device_SendCommand_HandCheck_Image1(1, 2);
                ac.SaveLog("用户按下向下按钮，event:1,type:2");
                down.setBackgroundResource(R.drawable.button11);
                return true;
            } else if (event.getAction() == MotionEvent.ACTION_UP || event.getAction() == MotionEvent.ACTION_CANCEL || event.getAction() == MotionEvent.ACTION_OUTSIDE) {
                ac.Device_SendCommand_HandCheck_Image1(2, 2);
                ac.SaveLog("用户松开向下按钮，event:2,type:2");
                down.setBackgroundResource(R.drawable.button13);
                return true;
            } else if (event.getAction() == MotionEvent.ACTION_MOVE) {
                // 忽略移动事件，不执行任何操作
                return true;
            }
            return false;
        }
        //向前
        else if (v.getId() == R.id.manual_cheak_front_button) {
            if (event.getAction() == MotionEvent.ACTION_DOWN) {
                ac.Device_SendCommand_HandCheck_Image2(1, 1);
                ac.SaveLog("用户按下向前按钮，event:1,type:1");
                front.setBackgroundResource(R.drawable.button11);
                return true;
            } else if (event.getAction() == MotionEvent.ACTION_UP || event.getAction() == MotionEvent.ACTION_CANCEL || event.getAction() == MotionEvent.ACTION_OUTSIDE) {
                ac.Device_SendCommand_HandCheck_Image2(2, 1);
                ac.SaveLog("用户松开向前按钮，event:2,type:1");
                front.setBackgroundResource(R.drawable.button13);
                return true;
            } else if (event.getAction() == MotionEvent.ACTION_MOVE) {
                // 忽略移动事件，不执行任何操作
                return true;
            }
            return false;
        }
        //向后
        else if (v.getId() == R.id.manual_cheak_after_button) {
            if (event.getAction() == MotionEvent.ACTION_DOWN) {
                ac.Device_SendCommand_HandCheck_Image2(1, 2);
                ac.SaveLog("用户按下向后按钮，event:1,type:2");
                after.setBackgroundResource(R.drawable.button11);
                return true;
            } else if (event.getAction() == MotionEvent.ACTION_UP || event.getAction() == MotionEvent.ACTION_CANCEL || event.getAction() == MotionEvent.ACTION_OUTSIDE) {
                ac.Device_SendCommand_HandCheck_Image2(2, 2);
                ac.SaveLog("用户松开向后按钮，event:2,type:2");
                after.setBackgroundResource(R.drawable.button13);
                return true;
            } else if (event.getAction() == MotionEvent.ACTION_MOVE) {
                // 忽略移动事件，不执行任何操作
                return true;
            }
            return false;
        }
        //机械臂往上
        else if (v.getId() == R.id.manual_cheak_arm_up_button) {
            if (event.getAction() == MotionEvent.ACTION_DOWN) {
                ac.Device_SendCommand_HandCheck_Image345(1, 1);
                ac.SaveLog("用户按下机械臂往上按钮，event:1,type:1");
                arm_up.setBackgroundResource(R.drawable.button11);
                return true;
            } else if (event.getAction() == MotionEvent.ACTION_UP || event.getAction() == MotionEvent.ACTION_CANCEL || event.getAction() == MotionEvent.ACTION_OUTSIDE) {
                ac.Device_SendCommand_HandCheck_Image345(2, 1);
                ac.SaveLog("用户松开机械臂往上按钮，event:2,type:1");
                arm_up.setBackgroundResource(R.drawable.button13);
                return true;
            } else if (event.getAction() == MotionEvent.ACTION_MOVE) {
                // 忽略移动事件，不执行任何操作
                return true;
            }
            return false;
        }
        //机械臂往下
        else if (v.getId() == R.id.manual_cheak_arm_dowm_button) {
            if (event.getAction() == MotionEvent.ACTION_DOWN) {
                ac.Device_SendCommand_HandCheck_Image345(1, 2);
                ac.SaveLog("用户按下机械臂往下按钮，event:1,type:2");
                arm_down.setBackgroundResource(R.drawable.button11);
                return true;
            } else if (event.getAction() == MotionEvent.ACTION_UP || event.getAction() == MotionEvent.ACTION_CANCEL || event.getAction() == MotionEvent.ACTION_OUTSIDE) {
                ac.Device_SendCommand_HandCheck_Image345(2, 2);
                ac.SaveLog("用户松开机械臂往下按钮，event:2,type:2");
                arm_down.setBackgroundResource(R.drawable.button13);
                return true;
            } else if (event.getAction() == MotionEvent.ACTION_MOVE) {
                // 忽略移动事件，不执行任何操作
                return true;
            }
            return false;
        }
        //锅往上
        else if (v.getId() == R.id.manual_cheak_pot_up_button) {
            if (event.getAction() == MotionEvent.ACTION_DOWN) {
                ac.Device_SendCommand_HandCheck_Image345(1, 3);
                ac.SaveLog("用户按下锅往上按钮，event:1,type:3");
                pot_up.setBackgroundResource(R.drawable.button11);
                return true;
            } else if (event.getAction() == MotionEvent.ACTION_UP || event.getAction() == MotionEvent.ACTION_CANCEL || event.getAction() == MotionEvent.ACTION_OUTSIDE) {
                ac.Device_SendCommand_HandCheck_Image345(2, 3);
                ac.SaveLog("用户松开锅往上按钮，event:2,type:3");
                pot_up.setBackgroundResource(R.drawable.button13);
                return true;
            } else if (event.getAction() == MotionEvent.ACTION_MOVE) {
                // 忽略移动事件，不执行任何操作
                return true;
            }
            return false;
        }
        //锅往下
        else if (v.getId() == R.id.manual_cheak_pot_down_button) {
            if (event.getAction() == MotionEvent.ACTION_DOWN) {
                ac.Device_SendCommand_HandCheck_Image345(1, 4);
                ac.SaveLog("用户按下锅往下按钮，event:1,type:4");
                pot_down.setBackgroundResource(R.drawable.button11);
                return true;
            } else if (event.getAction() == MotionEvent.ACTION_UP || event.getAction() == MotionEvent.ACTION_CANCEL || event.getAction() == MotionEvent.ACTION_OUTSIDE) {
                ac.Device_SendCommand_HandCheck_Image345(2, 4);
                ac.SaveLog("用户松开锅往下按钮，event:2,type:4");
                pot_down.setBackgroundResource(R.drawable.button13);
                return true;
            } else if (event.getAction() == MotionEvent.ACTION_MOVE) {
                // 忽略移动事件，不执行任何操作
                return true;
            }
            return false;
        }
        //转锅顺时针
        else if (v.getId() == R.id.manual_cheak_clockwise_button) {
            if (event.getAction() == MotionEvent.ACTION_DOWN) {
                ac.Device_SendCommand_HandCheck_Image345(1, 5);
                ac.SaveLog("用户按下转锅顺时针按钮，event:1,type:5");
                clockwise.setBackgroundResource(R.drawable.button11);
                return true;
            } else if (event.getAction() == MotionEvent.ACTION_UP || event.getAction() == MotionEvent.ACTION_CANCEL || event.getAction() == MotionEvent.ACTION_OUTSIDE) {
                ac.Device_SendCommand_HandCheck_Image345(2, 5);
                ac.SaveLog("用户松开转锅顺时针按钮，event:2,type:5");
                clockwise.setBackgroundResource(R.drawable.button13);
                return true;
            } else if (event.getAction() == MotionEvent.ACTION_MOVE) {
                // 忽略移动事件，不执行任何操作
                return true;
            }
            return false;
        }
        //转锅逆时针
        else if (v.getId() == R.id.manual_cheak_anticlockwise_button) {
            if (event.getAction() == MotionEvent.ACTION_DOWN) {
                ac.Device_SendCommand_HandCheck_Image345(1, 6);
                ac.SaveLog("用户按下转锅逆时针按钮，event:1,type:6");
                anticlockwise.setBackgroundResource(R.drawable.button11);
                return true;
            } else if (event.getAction() == MotionEvent.ACTION_UP || event.getAction() == MotionEvent.ACTION_CANCEL || event.getAction() == MotionEvent.ACTION_OUTSIDE) {
                ac.Device_SendCommand_HandCheck_Image345(2, 6);
                ac.SaveLog("用户松开转锅逆时针按钮，event:2,type:6");
                anticlockwise.setBackgroundResource(R.drawable.button13);
                return true;
            } else if (event.getAction() == MotionEvent.ACTION_MOVE) {
                // 忽略移动事件，不执行任何操作
                return true;
            }
            return false;
        }
        return false;
    }

    //显示弹窗
    private void showCleaningPipeDialog() {
        // 如果弹窗已经显示，则不重复显示
        if (dialog != null && dialog.isShowing()) {
            return;
        }
        // 创建自定义对话框
        dialog = new Dialog(this);
        dialog.setContentView(R.layout.cleaning_pipe_dialog); // 设置对话框的布局

        TextView titl = dialog.findViewById(R.id.cleaning_pipe_dialog_title);
        Button cancle = dialog.findViewById(R.id.cleaning_pipe_dialog_cancle);
        Button comfirm = dialog.findViewById(R.id.cleaning_pipe_dialog_confirm);
        if (cheak == 1){
            titl.setText("手动自检成功！");
        } else if (cheak == 2) {
            titl.setText("自检超时！");
        }
        cancle.setText("返回");
        comfirm.setText("前往首页");
        cancle.setOnClickListener(v -> {
            dialog.dismiss(); // 关闭弹窗
            // 更新视图的可见性
            box1.setVisibility(View.VISIBLE);
            box2.setVisibility(View.GONE);
            box3.setVisibility(View.GONE);
        });

        comfirm.setOnClickListener(v -> {
            startActivity(new Intent(this, HomeActivity.class));
            dialog.dismiss();
        });

        // 关键代码：设置窗口背景透明
        if (dialog.getWindow() != null) {
            dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        }

        // 设置点击外部不可取消对话框
        dialog.setCanceledOnTouchOutside(false);

        // 显示对话框
        dialog.show();
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void OnEventBusMassage(EventMessage event){
        if (event.id == EventMessage.MsgID_CheckSelf_Reset0){//手动自检的机械臂复位通知
            condition3.setVisibility(View.VISIBLE);
            int colorSelectedText = ContextCompat.getColor(this, R.color.green_color);
            condition3.setTextColor(colorSelectedText);
            condition3.setText("成功");
            ac.SaveLog("已收到手动自检的机械臂复位通知");
        } else if (event.id == EventMessage.MsgID_CheckSelf_Reset1) {//手动自检的锅口复位通知
            condition4.setVisibility(View.VISIBLE);
            int colorSelectedText = ContextCompat.getColor(this, R.color.green_color);
            condition4.setTextColor(colorSelectedText);
            condition4.setText("成功");
            ac.SaveLog("已收到手动自检的锅口复位通知");
        } else if (event.id == EventMessage.MsgID_CheckSelf_Reset2) {//手动自检的投菜复位通知
            condition1.setVisibility(View.VISIBLE);
            int colorSelectedText = ContextCompat.getColor(this, R.color.green_color);
            condition1.setTextColor(colorSelectedText);
            condition1.setText("成功");
            ac.SaveLog("已收到手动自检的投菜复位通知");
        } else if (event.id == EventMessage.MsgID_CheckSelf_Reset3) {//料盒轨道平移复位通知
            condition2.setVisibility(View.VISIBLE);
            int colorSelectedText = ContextCompat.getColor(this, R.color.green_color);
            condition2.setTextColor(colorSelectedText);
            condition2.setText("成功");
            ac.SaveLog("已收到料盒轨道平移复位通知");
        } else if (event.id == EventMessage.MsgID_CheckSelf_Reset4) {//已收到自动寻位消息
            condition5.setVisibility(View.VISIBLE);
            int colorSelectedText = ContextCompat.getColor(this, R.color.green_color);
            condition5.setTextColor(colorSelectedText);
            condition5.setText("成功");
            ac.SaveLog("已收到自动寻位消息通知");
            cheak = 1;
            showCleaningPipeDialog();//显示弹窗
        } else if (event.id == EventMessage.MsgID_CheckSelf_Reset5) {//收到自动寻位超时的消息后
            ac.SaveLog("已收到自动寻位超时消息通知");
            cheak = 2;
            showCleaningPipeDialog();//显示弹窗
        }
    }
}